import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { post,get } from '@/utils/request'
import { useAuthStore, useSettingStore } from '@/store'

export function fetchChatAPI<T = any>(
  prompt: string,
  options?: { conversationId?: string; parentMessageId?: string },
  signal?: GenericAbortSignal,
) {
  return post<T>({
    url: '/chat',
    data: { prompt, options },
    signal,
  })
}

export function fetchChatConfig<T = any>() {
  return post<T>({
    url: '/config',
  })
}

//本地node对接chatGpt
export function fetchChatAPIProcess<T = any>(
  params: {
    prompt: string
    options?: { conversationId?: string; parentMessageId?: string }
    signal?: GenericAbortSignal
    onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
) {
  const settingStore = useSettingStore()
  const authStore = useAuthStore()

  let data: Record<string, any> = {
    prompt: params.prompt,
    options: params.options,
  }

  if (authStore.isChatGPTAPI) {
    data = {
      ...data,
      systemMessage: settingStore.systemMessage,
      temperature: settingStore.temperature,
      top_p: settingStore.top_p,
    }
  }

  return post<T>({
    url: '/chat-process',
    data,
    signal: params.signal,
    onDownloadProgress: params.onDownloadProgress,
  })
}




export function fetchSession<T>() {
  return post<T>({
    url: '/session',
  })
}

export function fetchVerify<T>(token: string) {
  return post<T>({
    url: '/verify',
    data: { token },
  })
}

//推荐列表分页
export function recommendPage<T>(params:any) {
  return post<T>({
    url: '/home/recommend/recommendPage',
    data: params,
  })
}

//删除对话
export function removeByIds<T>(params:any) {
  return post<T>({
    url: '/dialog/removeByIds',
    data: params,
  })
}

//分页查询对话
export function dialogList<T>(params:any) {
  return post<T>({
    url: '/dialog/page',
    data: params,
  })
}


//保存对话
export function dialogSave<T>(params:any) {
  return post<T>({
    url: '/dialog/save',
    data: params,
  })
}
//JAVA后台接口生成对话
export function fetchJavaChatAPIProcess<T = any>(
  params: {
    dialogId: string
    prompt: string
    saveUserInput?:boolean
    options?: { conversationId?: string; parentMessageId?: string }
    contentBlockId:string
    signal?: GenericAbortSignal
    reqType:number
    reGen:boolean
    type:number
    form:FormData
    onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
) {

  let data: Record<string, any> = {
    dialogId:params.dialogId,
    userInput:params.prompt,
    saveUserInput:params.saveUserInput,
    reqType:params.reqType,
    contentBlockId:params.contentBlockId,
    reGen:params.reGen,
  }
  let form = params.form
  let api = ''
  switch (params.type) {
    case 1:
      api = '/dialog/content/generateContentStream'
      break;
    case 2:
      api = '/dialog/content/generateReportStream'
      break;
    case 3:
      api = '/dialog/content/uploadAndRespOutline'
      form.append("dialogId", params.dialogId);
      break;
    case 4:
      api = '/dialog/content/fleshReportStream'
      break;
    default:
      break;
  }
  return post<T>({
    url: api,
    data:params.type!=3?data:form,
    headers: {
      "Content-Type":params.type!=3?"application/json":"multipart/form-data"
    },
    signal: params.signal,
    onDownloadProgress: params.onDownloadProgress,
  })
}

//查询对话内容
export function pageContent<T>(params:any) {
  return post<T>({
    url: '/dialog/content/pageContent',
    data: params,
  })
}

//生成对话内容
export function generateContent<T>(params:any) {
  return post<T>({
    url: '/dialog/content/generateContent',
    data: params,
  })
}//保存对话内容
export function pageContentSave<T>(params:any) {
  return post<T>({
    url: '/dialog/content/save',
    data: params,
  })
}

//下载文件
export function downloadFile<T>(params:any) { 
  return get<T>({ url: '/dialog/content/download', 
    data: params, 
    headers:{
      responseType: 'blob' 
    }
  }) 
}
//删除对话块
export function contentRemoveByIds<T>(params:any) { 
  return post<T>({
     url: '/dialog/content/removeByIds', 
    data: params, 
  }) 
}


//上传文件
export function uploadFile<T>(params:any) { 

  return post<T>({ url: '/dialog/content/upload', 
    data: params, 
    
    headers: {
      "Content-Type": "multipart/form-data"
    },
  }) }

    